package com.py.credit.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.OrgTableFieldEnum;
import com.py.credit.entity.enums.ResourceEvalInfoFieldsEnum;
import com.py.credit.service.OrgService;
import com.py.credit.service.XypjRemoteBaseService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

/**
 * 评价流程前台
 * 
 * @author dd
 *
 */
@Api(tags = "activity", description = "后台评价流程")
@RestController
@RequestMapping(value = "/activity")
public class ActivityController {

	private final static String FRONT_B_TABLE_ID = "bCommitmentActivityTable";

	@Autowired
	private OrgService orgService;

	@Autowired
	private XypjRemoteBaseService xypjRemoteBaseService;

	@RequestMapping(value = "/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {
		return new RestServiceResult<List<OptionalColumn>>(ResourceEvalInfoFieldsEnum.getOptionalColumns());

	}

	@RequestMapping(value = "/getAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {

		return new RestServiceResult<List<OptionalColumn>>(ResourceEvalInfoFieldsEnum.getAdvancedConditionColumns());

	}

	@RequestMapping(value = "/addBColumns.json")
	public RestServiceResult<String> addBShowColunms(HttpServletRequest request,
			@RequestParam(value = "columns", required = true) String[] columns) {
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo && userInfo.getId() != null) {

				MapCache.set(userInfo.getId() + "_" + FRONT_B_TABLE_ID, columns);
			} else {

				ExceptionLogger.error("ActivityController#addBColumns", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ActivityController#addBColumns", "未获取到当前用户信息！");
		}

		return new RestServiceResult<String>();

	}

	@RequestMapping(value = "/getBTableList.json")
	public RestServiceResult<List<Map<String, Object>>> getBTableList(
			@RequestParam(value = "condition", defaultValue = "") String condition, String shzt, Page page,
			HttpServletRequest request) {

		String[] configColumns = null;

		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_B_TABLE_ID);

			} else {

				ExceptionLogger.error("ActivityController#getBTableList", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ActivityController#getBTableList", "未获取到当前用户信息！");
		}

		String[] additionalColumns = { "id", "modelId", "sqr" };

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? OrgTableFieldEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);

		List<Map<String, Object>> tableList = orgService.getActivityByConditionForPage(showColumns, additionalColumns,
				condition, page, null, null);

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

	}

	@RequestMapping(value = "/bExport")
	public void bExport(
			@ApiParam(required = true, name = "condition", value = "搜索条件") @RequestParam(value = "condition", defaultValue = "", required = true) String condition,
			@ApiParam(required = true, name = "columnEN", value = "导出列") @RequestParam(value = "columnEN", required = true) String columnEN,
			Page page, HttpServletResponse response, HttpServletRequest request) throws Exception {

		page.setLimit(1000);

		List<String> columns = JSONArray.parseArray(columnEN, String.class);

		List<Map<String, Object>> data = orgService.getActivityByConditionForPage(columns, null, condition, page, null,
				null);

		List<OptionalColumn> optionalColumns = ResourceEvalInfoFieldsEnum.getOptionalColumns();

		ExcelUtil.downloadExcel(response, data, "审核列表", "审核列表", columns, optionalColumns);

	}

}
